package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.entity.RequestData;
import org.dey.mapper.MemberMapper;
import org.dey.pojo.Member;
import org.dey.pojo.User;
import org.dey.service.MemberService;
import org.dey.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author deyran
 * @date 2022/3/31 15:14
 * @classname
 * @see
 */
@Service
@Transactional
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserService userService;

    @Override
    public PageInfo<Member> memberList(RequestData<Member> requestData) {

        User user = userService.getUser(requestData.getOneRequirement());
        List<User> users = new ArrayList<>();
        users.add(user);

        PageHelper.startPage(requestData.getPageNum(), requestData.getPageSize());
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("id","SELECT member_id FROM t_member_user WHERE user_id = "+user.getId()+"");
        if (StringUtils.isNotBlank(requestData.getRequirement().getName())) {
            queryWrapper.like("name",requestData.getRequirement().getFileNumber());
        }
        if (StringUtils.isNotBlank(requestData.getRequirement().getFileNumber())){
            queryWrapper.or().like("file_number",requestData.getRequirement().getFileNumber());
        }
        List<Member> members = this.list(queryWrapper);
        for (Member member : members) {
            member.setUsers(users);
        }
        return new PageInfo<>(members);
    }
}
